#include "ope.h"

void staff_open(int sockfd,MSG *msg,USER *user){
    int choice;
    printf("\n***员工你好 欢迎登陆员工管理系统***\n\n");
    while (1)
    {
        printf("********************************************\n");
		printf("********************************************\n");
		printf("**     1.查询   2.修改   3.退出           **\n");
		printf("********************************************\n");
		printf("********************************************\n");
		printf("请输入您的选择（数字）>>");
        scanf("%d",&choice);
        getchar();
        switch(choice)
        {
            case 1:
                staff_search(user);
                break;
            case 2:
                staff_rem(sockfd,msg,user);
                break;
            case 3:
                printf("再见\n");
                sleep(1);
                return;
            default:
                printf("输入错误，重新选择\n");
        }
    }
    
}

void staff_search(USER *user){
    printf("\n*********************************************************************\n");
	printf("工号\t姓名\t密码\t类型\t级别\t年龄\t住址\t电话\t工资\n");
	printf("%d\t%s\t%s\t",user->job,user->name,user->pwd);
	printf("%c\t%d\t%d\t",user->type,user->level,user->age);
	printf("%s\t%s\t%.1lf\n",user->addr,user->phone,user->salary);
	printf("*********************************************************************\n\n");
}

void staff_rem(int sockfd,MSG *msg,USER *user){
    int choice;
    USER_UNION user_union;
    while (1)
    {
        printf("********************************************\n");
		printf("********************************************\n");
		printf("**    1.住址    2.密码   3.电话    4.姓名    **\n");
        printf("********************************************\n");
		printf("********************************************\n");
		printf("请输入您的选择（数字）>>");
        scanf("%d",&choice);
        getchar();
        switch(choice)
        {
            case 1:
                printf("请输入新地址》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_ADDR;
                strcpy(msg->Usemsg.addr,(char *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的地址： %s\n",user->addr);
                break;
            case 2:
                printf("请输入新密码》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_PWD;
                strcpy(msg->Usemsg.pwd,(char *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的密码： %s\n",user->pwd);
                break;
            case 3:
                printf("请输入新号码》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_PHONE;
                strcpy(msg->Usemsg.phone,(char *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的新号码： %s\n",user->phone);
                break;
            case 4:
                printf("请输入新姓名》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_NAME;
                strcpy(msg->Usemsg.name,(char *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的姓名： %s\n",user->name);
                break;
            case 5:
                printf("请输入新年龄》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_AGE;
                msg->Usemsg.age = (*(int *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的年龄： %d\n",user->age);
                break;
            case 6:
                printf("请输入新工资》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_SASLARY;
                msg->Usemsg.salary = *((double *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的工资： %f\n",user->salary);
                break;
            case 7:
                printf("请输入新评级》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_SASLARY;
                msg->Usemsg.salary = *((int *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的评级： %d\n",user->level);
                break;
            case 8:
                printf("退出修改\n");
                sleep(1);
                return;
        }
    }
    
}

void admin_open(int sockfd,MSG *msg,USER *user){
    int choice;
    printf("\n***管理员你好 欢迎登陆员工管理系统***\n\n");
    while(1){
        printf("********************************************\n");
		printf("********************************************\n");
		printf("**     1.添加   2.删除   3.修改           **\n");
		printf("**     4.查询   5.历史   6.退出           **\n");
		printf("********************************************\n");
		printf("********************************************\n");
		printf("请输入您的选择（数字）>>");

        scanf("%d",&choice);
        getchar();
        switch(choice)
        {
            case 1:
                admin_add(sockfd,msg,user);
                break;
            case 2:
                admin_del(sockfd,msg,user);
                break;
            case 3:
                admin_rem(sockfd,msg,user);
                break;
            case 4:
                admin_search(sockfd,msg,user);
                break;
            case 5:
                admin_history(sockfd,msg,user);
                break;
            case 6:
                printf("再见\n");
                return;
            default:
                printf("输入错误\n");
        }
    }
}

void admin_add(int sockfd,MSG *msg,USER *user){
    printf("****欢迎新员工****\n");
    printf("输入工号：");
    scanf("%d",&(msg->Usemsg.job));
    getchar();

    printf("\n请输入姓名：");
	scanf("%s",msg->Usemsg.name);
	getchar();
	
	printf("\n请输入密码）：");
	scanf("%s",msg->Usemsg.pwd);
	getchar();
	
	printf("\n请输入类型：");
	scanf("%c",&(msg->Usemsg.type));
	getchar();
	
	printf("\n请输入等级：");
	scanf("%d",&(msg->Usemsg.level));
	getchar();
	
	printf("\n请输入年龄：");
	scanf("%d",&(msg->Usemsg.age));
	getchar();
	
	printf("\n请输入地址：");
	scanf("%s",msg->Usemsg.addr);
	getchar();
	
	printf("\n请输入电话：");
	scanf("%s",msg->Usemsg.phone);
	getchar();
	
	printf("\n请输入工资：");
	scanf("%lf",&(msg->Usemsg.salary));
	getchar();

    msg->ope = INC;
    printf("新增员工信息确认\n");
    staff_search(&msg->Usemsg);
    send(sockfd,msg,sizeof(MSG),0);
    printf("新增员工成功\n");
    memset(user,0,sizeof(USER));
	recv(sockfd,user,sizeof(USER),0);
	
	staff_search(user);
}

void admin_del(int sockfd,MSG *msg,USER *user){
    printf("请输入删除的工号：");
    scanf("%d",&(msg->Usemsg.job));
    msg->ope = DEL;
    send(sockfd,msg,sizeof(MSG),0);
    memset(user,0,sizeof(USER));
    recv(sockfd,user,sizeof(USER),0);
    printf("删除员工信息请确认：\n");
    staff_search(user);
    printf("删除成功\n");
}

void admin_rem(int sockfd,MSG *msg,USER *user){
    int choice;
    USER_UNION user_union;
    while (1)
    {
        printf("********************************************\n");
		printf("********************************************\n");
		printf("**    1.住址    2.密码   3.电话    4.姓名    **\n");
		printf("**    5.年龄    6.工资   7.评级    8.退出    **\n");
        printf("********************************************\n");
		printf("********************************************\n");
		printf("请输入您的选择（数字）>>");
        scanf("%d",&choice);
        getchar();
         switch(choice)
        {
            case 1:
                printf("请输入要修改员工的工号》》");
                scanf("%d",&msg->Usemsg.job);
                printf("\n****************************************\n");
                printf("请输入新地址》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_ADDR;
                strcpy(msg->Usemsg.addr,(char *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的地址： %s\n",user->addr);
                break;
            case 2:
                printf("请输入要修改员工的工号》》");
                scanf("%d",&msg->Usemsg.job);
                printf("\n****************************************\n");
                printf("请输入新地址》》");
                printf("请输入新密码》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_PWD;
                strcpy(msg->Usemsg.pwd,(char *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的密码： %s\n",user->pwd);
                break;
            case 3:
                printf("请输入要修改员工的工号》》");
                scanf("%d",&msg->Usemsg.job);
                printf("\n****************************************\n");
                printf("请输入新号码》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_PHONE;
                strcpy(msg->Usemsg.phone,(char *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的新号码： %s\n",user->phone);
                break;
            case 4:
                printf("请输入要修改员工的工号》》");
                scanf("%d",&msg->Usemsg.job);
                printf("\n****************************************\n");
                printf("请输入新姓名》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_NAME;
                strcpy(msg->Usemsg.name,(char *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的姓名： %s\n",user->name);
                break;
            case 5:
                printf("请输入要修改员工的工号》》");
                scanf("%d",&msg->Usemsg.job);
                printf("\n****************************************\n");
                printf("请输入新年龄》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_AGE;
                msg->Usemsg.age = (*(int *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的年龄： %d\n",user->age);
                break;
            case 6:
                printf("请输入要修改员工的工号》》");
                scanf("%d",&msg->Usemsg.job);
                printf("\n****************************************\n");
                printf("请输入新工资》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_SASLARY;
                msg->Usemsg.salary = *((double *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的工资： %f\n",user->salary);
                break;
            case 7:
                printf("请输入要修改员工的工号》》");
                scanf("%d",&msg->Usemsg.job);
                printf("\n****************************************\n");
                printf("请输入新评级》》");
                scanf("%s",(char *)&user_union);
                msg->ope = REM_SASLARY;
                msg->Usemsg.level = *((int *)&user_union);
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                printf("修改后的评级： %d\n",user->level);
                break;
            case 8:
                printf("退出修改\n");
                sleep(1);
                return;
            default:
                printf("请重新输入\n");
        }
    }
    
}

void admin_search(int sockfd,MSG *msg,USER *user){
    int choice;
    while (1)
    {
        printf("********************************************\n");
		printf("**    1.查询个人    2.查询所有    3.退出      **\n");
		printf("********************************************\n");
		printf("请输入您的选择（数字）>>");
		scanf("%d",&choice);
		getchar();  
        switch(choice)
        {
            case 1:
                msg->ope = FIND;
                printf("请输入查找的工号: ");
                scanf("%d",&(msg->Usemsg.job));
                send(sockfd,msg,sizeof(MSG),0);
                memset(user,0,sizeof(USER));
                recv(sockfd,user,sizeof(USER),0);
                if(user->type == '3'){
                    printf("未找到\n");
                    break;
                }
               // staff_search(user);
            case 2:
            msg->ope = FINDALL;
            send(sockfd,msg,sizeof(MSG),0);
            printf("\n*********************************************************************\n");
			printf("工号\t姓名\t密码\t类型\t级别\t年龄\t住址\t电话\t工资\n"); 
            while(1)
				{
					recv(sockfd,user,sizeof(USER),0);
					if(user->type == 0)
						break;
					printf("%d\t%s\t%s\t",user->job,user->name,user->pwd);
					printf("%c\t%d\t%d\t",user->type,user->level,user->age);
					printf("%s\t%s\t%.1lf\n",user->addr,user->phone,user->salary);
					memset(user,0,sizeof(USER));
				}
				printf("*********************************************************************\n\n");
				break;
			case 3:
				printf("退出查询\n");
				sleep(1);
				return;
			default:
				printf("输入错误，请重新选择!\n");
        }  
    }
    
}

void admin_history(int sockfd,MSG *msg,USER *user){
    msg->ope = HISTORY;
    send(sockfd,msg,sizeof(msg),0);
    memset(user,0,sizeof(USER));
    printf("*********************************************\n");
    while(1){
        recv(sockfd,user,sizeof(USER),0);
        if(user->type == 0)
            break;
        printf("%s",user->des);
        memset(user,0,sizeof(USER));
    }
    printf("*********************************************\n");
}

void login_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    time(&((THREAD_PASS *)arg)->t);
    ((THREAD_PASS *)arg)->lt = localtime(&((THREAD_PASS *)arg)->t);

    sprintf(sql,"select * from employee where job=%d and pwd='%s'",msg->Usemsg.job,msg->Usemsg.pwd);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg)!= SQLITE_OK){
        printf("error: %s\n", errmsg);
    }else{
        if(nrow == 0){
            user.type = '3';
            strcpy(user.des,"name or passwd failed\n");
            
            send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
            
        }else{
            user.job = msg->Usemsg.job;
            strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
            send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
            sprintf(sql,"%d-%02d-%02d %02d-%02d-%02d  %d %s login\n",
				((THREAD_PASS *)arg)->lt->tm_year+1900,((THREAD_PASS *)arg)->lt->tm_mon+1,
				((THREAD_PASS *)arg)->lt->tm_mday,((THREAD_PASS *)arg)->lt->tm_hour,
				((THREAD_PASS *)arg)->lt->tm_min,((THREAD_PASS *)arg)->lt->tm_sec,
				user.job,user.name);
            write(((THREAD_PASS *)arg)->fd,sql,strlen(sql));
        }
    }

}

void inc_fun(void *arg,MSG *msg){
    char sql[500];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"insert into employee values(%d,\"%s\",\"%s\",\"%c\",%d,%d,\"%s\",\"%s\",%lf);",msg->Usemsg.job,msg->Usemsg.name,msg->Usemsg.pwd,msg->Usemsg.type,msg->Usemsg.level,msg->Usemsg.age,msg->Usemsg.addr,msg->Usemsg.phone,msg->Usemsg.salary);
	printf("%s\n",sql);
     if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    memset(sql,0,sizeof(sql));

    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void del_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}

    sprintf(sql,"delete from employee where no=%d;",msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
}

void rem_pwd_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"update employee set pwd='%s' where job=%d;",msg->Usemsg.pwd,msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void rem_addr_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"update employee set addr='%s' where job=%d;",msg->Usemsg.addr,msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void rem_phone_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"update employee set phone='%s' where job=%d;",msg->Usemsg.phone,msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void rem_name_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"update employee set name='%s' where job=%d;",msg->Usemsg.name,msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void rem_saslary_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"update employee set salary=%lf where job=%d;",msg->Usemsg.salary,msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void rem_age_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"update employee set age=%d where job=%d;",msg->Usemsg.age,msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void rem_level_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"update employee set level=%d where job=%d;",msg->Usemsg.level,msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void rem_find_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;

    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
			user.job = msg->Usemsg.job;
			strcpy(user.name,ret[10]);
			strcpy(user.pwd,ret[11]);
			user.type = ret[12][0];
			user.level = atoi(ret[13]);
			user.age = atoi(ret[14]);
			strcpy(user.addr,ret[15]);
			strcpy(user.phone,ret[16]);
			user.salary = atoi(ret[17]);
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}
	}
}

void rem_findall_fun(void *arg,MSG *msg){
    char sql[128];
    char **ret;
    int nrow, ncolumn;
    char *errmsg;
    USER user;
    int i;

    sprintf(sql,"select * from employee where job=%d;",msg->Usemsg.job);
    if(sqlite3_exec(((THREAD_PASS *)arg)->db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error: %s\n", errmsg);
    }
    if(sqlite3_get_table(((THREAD_PASS *)arg)->db,sql,&ret,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
	{
		printf("---****----%s.\n",errmsg);		
	}
	else
	{		
		if(nrow == 0){
			user.type = '3';
			strcpy(user.des,"failed.\n");
			send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		}else{
                for(i=0;i<nrow;i++){
                user.job =atoi(ret[9+i*9]);
                strcpy(user.name,ret[10+i*9]);
                strcpy(user.pwd,ret[11+i*9]);
                user.type = ret[12+i*9][0];
                user.level = atoi(ret[13+i*9]);
                user.age = atoi(ret[14+i*9]);
                strcpy(user.addr,ret[15+i*9]);
                strcpy(user.phone,ret[16+i*9]);
                user.salary = atoi(ret[17+i*9]);
                send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
            }
            user.type = 0;
            strcpy(user.des,"over\n");
            send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
        }
	}
}

void rem_history_fun(void *arg,MSG *msg){
    int readbyte;
	USER user;
	printf("history_handle\n");
	lseek(((THREAD_PASS *)arg)->fd, 0, SEEK_SET);
	while((readbyte = read(((THREAD_PASS *)arg)->fd,user.des,64))>0)
	{
		user.des[readbyte] = '\0';
		send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
		usleep(50);
	}
	user.type = 0;
	strcpy(user.des,"over");
	send(((THREAD_PASS *)arg)->accept_fd,&user,sizeof(USER),0);
	usleep(50);
}
